<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    // 1.利用递归求两个数的最大公约数
    function greatest_common_divisor(m, n) {
      let r = m % n;
      m = n;
      n = r;
      if (r == 0) {
        return m;
      }
      return greatest_common_divisor(m, n);
    }
    document.write("最大公约数：" + greatest_common_divisor(2, 8) + "<br>");

    // 2.编写任意个数字的求和、差、积、商的函数
    function rubbish() {
      let he = 0,
        cha = 0,
        ji = 1,
        shang = 1;
      for (let i = 0; i < arguments.length; i++) {
        he += arguments[i];
        ji *= arguments[i];
        if (i == 0) {
          cha = arguments[i];
          shang = arguments[i];
        } else {
          cha -= arguments[i];
          shang /= arguments[i];
        }
      }
      return `和为：${he}；差为：${cha}；积为：${ji}；商为：${shang}`;
    }
    document.write(rubbish(2, 3, 4, 5) + "<br>");
    // 3.编写生成4位数字验证码的函数，并生成10次
    function code(n) {
      while (n > 0) {
        let str = "";
        for (let i = 0; i < 4; i++) {
          str += String(parseInt(Math.random() * 10));
        }
        n--;
        document.write("验证码：" + str + "<br>");
      }
    }
    code(10);
    // 4.编写一个函数，计算任意两个数字之间所能组成的奇数个数，数字必须是个位数
    //比如： 计算0-3之间能组成的奇数个是01、03、13、21、23、31
    function odd(num1, num2) {
      let str = "";
      if ((num1 > 10 && num1 <= 0) || (num2 > 10 && num2 <= 0)) {
        console.log("只能输入大于0的个位数：");
      } else {
        for (let i = num1; i <= num2; i++) {
          for (let j = num1; j <= num2; j++) {
            str = "";
            if (i != j) {
              str = String(i) + String(j);
              if (Number(str) % 2 !== 0) {
                document.write(str + "\t");
              }
            }
          }
        }
        document.write("<br>");
      }
    }
    odd(0, 9);
    // 5.加密规则如下：每位数字都加上5,然后用除以10的余数代替该数字，
    // 再将第一位和第四位交换，第二位和第三位交换，请编写一个函数，传入原文，输出密文
    function encryption(num) {
      let change = [],
        tmp = ""; //临时变量
      for (let i = 0; i < String(num).length; i++) {
        let sum = null;
        sum = (1 * String(num)[i] + 5) % 10;
        tmp += String(sum);
      }
      for (let j = tmp.length - 1; j >= 0; j--) {
        change += Number(tmp[j]);
        // console.log(tmp[j]);
      }
      return change;
    }
    document.write("加密：" + encryption(1280));
  </script>
</html>
